<?php

    $Familias = crearArbol(0);
    $DependeDe = $Familias;
?>    
<table width='1000'>
<tr>
	<td align='right'>
<?php if ($accion=='listar' and ( $_SESSION['god'] == 1 or $Config['crea_familias'] == 'S') ) { ?> 
       <a href="familias.php?accion=editar&id=0" class="boton1"  title='Crear nueva Familia de Productos' >Crear nueva Familia de Productos</a>
<?php } ?>    
	</td>
</tr>
</table>
<br />
<?php
	if (!empty($Familias)) { ?>
	<table width='1000' cellpadding='8' cellspacing='0' style="border-width:1px; border-style:solid; border-color:#000000">
		<tr >
			<td width='10' class="th"></td>
			<td width='80' class="th">Acciones</td>
			<td width='30' class="th" colspan="2">Orden</td>
			<td  class="th" width='170' colspan="5">Nombre</td>
            <td width='100' class="th" >Depende de...</td>
            <td  class="th">URL Amigable</td> 
            <td  class="th" width="10">Eliminar</td>
        </tr>
		<tr style="height:1px;">
			<td style="height:1px;"></td>
			<td ></td>
			<td width="5"></td>
            <td width="5"></td>  
			<td width="10"></td>
            <td width="40"></td>
            <td width="40"></td>
            <td width="40"></td>
            <td width="40"></td>
            <td></td>
            <td ></td> 
            <td ></td>
		</tr>
        
		<?php 
            $url = array();
            $color = '';
            foreach($Familias as $familia) {
                $url[$familia['nivel']] = $familia['urlamigable1'];
                
                if ($color=='#DFECFF')  {$color = '#D3DFF1';} else {$color = '#DFECFF';}
    		?>
    
    			<tr bgcolor='<?=$color;?>' >
    				<td align='center'>	
                        <?php echo $familia['id'];?>
<?php
                        $cond 	= "select * from familias_grupos where familia_id = '{$familia['id']}' ";
                		$rs 	= $db->Execute($cond);
                		$Pertenece	= $rs->GetRows();
                        if ($Pertenece) { echo "<img src='".ADMIN."img/candado.jpg' title='Acceso restringido a determinados grupos' border='0'/>";}

?>                         
                    </td>
                    
    				<td align='center'>
                        <a href='<?=ADMIN;?>familias.php?accion=editar&id=<?=$familia['id'];?>'
            			     title='Editar esta Familia'>
            			     <img src='<?=ADMIN;?>img/edit.gif' border='0' /></a>&nbsp;&nbsp;&nbsp;&nbsp;

                        <?php if ($familia['activo'] == 1) { $imagen="img/activo.gif"; } else { $imagen='img/inactivo.gif'; } ?>
        				<a href='familias.php?accion=estado&id=<?=$familia['id'];?>'
        				    title='Activar/Desactivar esta Familia'>
        					<img src='<?=ADMIN;?><?=$imagen;?>' border='0'/></a>
    				</td>
                    <!-- ORDEN -->
    				<td align='center' nowrap='nowrap'>
                        <?php if ($familia['orden'] != $familia['de']) { ?>
    					   <a href='familias_orden.php?sube=no&id=<?=$familia['id'];?>' title='Bajar de Orden' >
    						<img src='<?=ADMIN;?>img/abajo.gif' border='0' /></a>
                        <?php } ?>      
                    </td>
                    <td align='center' nowrap='nowrap'>
    					<?php if ($familia['orden']>1) { ?>
                            <a href='familias_orden.php?sube=si&id=<?=$familia['id'];?>' title='Subir de Orden' >
    						<img src='<?=ADMIN;?>img/arriba.gif' border='0' /></a>
                        <?php } ?>
    				</td>

    				<td colspan="<?php echo $familia['nivel'];?>"></td>
                    <td colspan="<?php echo (5-$familia['nivel']);?>"><?=$familia['nombre1'];?></td>
                    
                    <!-- DEPENDE DE... -->
                    <td >
<?php
                        $parent_id = $familia['parent_id'];
                        $depende_de = '<select name="depende_de" onChange="document.location.href=\'familias_nivel.php?id='.$familia['id'].'&new_parent=\'+this.value">';
                        $depende_de.= "<option value=0>Raiz 1er.Nivel</option>";
                        foreach($DependeDe as $dd){
                            if ($familia['id'] != $dd['id']) {                             
                                if ($parent_id == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
                                $separador = "";
                                if ($dd['nivel'] == 2 ) {
                                    $separador = "------";
                                } elseif ($dd['nivel'] == 3 ) {
                                    $separador = "------------";
                                } elseif ($dd['nivel'] == 4 ) {
                                    $separador = "------------------";
                                }


                                $depende_de.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre1']}</option>";
                            }
                        }
                        $depende_de.= "</select>";
?>


                        <?php echo $depende_de;?>
                    </td>
                    
                    <td >
                        <?php 
                            if ($familia['nivel'] == 1) { echo "/{$url[1]}";}
                            if ($familia['nivel'] == 2) { echo "/{$url[1]}/{$url[2]}";}
                            if ($familia['nivel'] == 3) { echo "/{$url[1]}/{$url[2]}/{$url[3]}";}
                            if ($familia['nivel'] == 4) { echo "/{$url[1]}/{$url[2]}/{$url[3]}/{$url[4]}";}
                        ?>
                    </td>
                    
                    <!-- DELETE -->
                    <td align="right">
<?php
                        $id = $familia['id'];
                        $sql  = "select * from familias where parent_id = '$id'";
                    	$rs   = $db->Execute($sql);
                    	$z_delete 	= $rs->GetRows();
                        if (!$z_delete) { ?>
        					<a href='<?=ADMIN;?>familias.php?accion=delete&id=<?php echo $familia['id'];?>'
        						title='Eliminar esta categoria'
        						onclick="return confirm('Est&aacute; seguro de eliminar esta Familia?');">
        						<img src='<?=ADMIN;?>img/del.gif' border='0' /></a>&nbsp;&nbsp;&nbsp;&nbsp;
                            
                        <?php } ?>                        
                    </td>                

    			</tr>
            <?php } ?>

	</table>
<?php } else { ?>
	<span class='titulo'>No hay Categorias para mostrar</span>
<?php } ?>